﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Serialization;

namespace WinFormsApp1
{
    public partial class Form1 : Form
    {
        public OrderExer.OrderService service = new OrderExer.OrderService();
       
        
        public Form1()
        {
            InitializeComponent();
        }

        //查询按钮
        private void button1_Click(object sender, EventArgs e)
        {
            int index = comboBox1.SelectedIndex;

            switch (index) 
            {
                //按照订单号选择
                case 0:
                    string s = "";
                    var byOrderNo = from x in service.orderList where x.OrderNo > 0 orderby x.Amount select x;
                    bindingSource1.DataSource = byOrderNo;
                    foreach (OrderExer.Order order in byOrderNo)
                    {
                        bindingSource2.DataSource = order.orderDetails;
                    }
                    foreach (OrderExer.Order order in byOrderNo)
                    {
                        s += order.ToString() + "\n";
                    }
                    label1.Text = s;
                    break;
                //按照商品名称选择
                case 1:
                    string s1 = "";
                    string cname2 = "";
                    //Console.WriteLine("输入商品名称");
                    Form2 form2 = new Form2();
                    form2.ShowDialog();
                    cname2 = form2.s;
                    var byOrderCname = from x in service.orderList where x.Cname == cname2 orderby x.Amount select x;
                    bindingSource1.DataSource = byOrderCname;
                    foreach (OrderExer.Order order in byOrderCname)
                    {
                        bindingSource2.DataSource = order.orderDetails;
                    }
                    foreach (OrderExer.Order order in byOrderCname)
                    {
                        s1 += order.ToString() + "\n";
                    }
                    label1.Text = s1;
                    break;
                //按照客户选择
                case 2:
                    string s2 = "";
                    //Console.WriteLine("输入客户名称");
                    string name2 = "";
                    Form2 form3 = new Form2();
                    form3.ShowDialog();
                    name2 = form3.s;
                    var byCustomerName = from x in service.orderList where x.Customername == name2 orderby x.Amount select x;
                    bindingSource1.DataSource = byCustomerName;
                    foreach (OrderExer.Order order in byCustomerName)
                    {
                        bindingSource2.DataSource = order.orderDetails;
                    }
                    foreach (OrderExer.Order order in byCustomerName)
                    {
                        s2 += order.ToString() + "\n";
                    }
                    label1.Text = s2;
                    break;
                //按照订单金额选择
                case 3:
                    string s3 = "";
                    //Console.WriteLine("输入金额");
                    int money = 0;
                    Form2 form4 = new Form2();
                    form4.ShowDialog();
                    money = int.Parse(form4.s);
                    var byAmount = from x in service.orderList where x.Amount == money orderby x.Amount select x;
                    bindingSource1.DataSource = byAmount;
                    foreach (OrderExer.Order order in byAmount)
                    {
                        bindingSource2.DataSource = order.orderDetails;
                    }
                    foreach (OrderExer.Order order in byAmount)
                    {
                        s3 += order.ToString() + "\n";
                    }
                    label1.Text = s3;
                    break;
                default:break;
            }

        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            //OrderExer.Order o1 = new OrderExer.Order(1, "1", "1", 1, 1);
            //OrderExer.Order o2 = new OrderExer.Order(2, "2", "2", 2, 1);
            //service.addOrder(o1);
            FormAdd formadd = new FormAdd();
            formadd.ShowDialog();
            OrderExer.Order o1 = new OrderExer.Order(formadd.orderNo, formadd.customername, formadd.cname, formadd.amount, formadd.numbers);
            bindingSource1.DataSource = o1;
 
               
            
            o1.orderDetails = formadd.orderDetails;
            bindingSource2.DataSource = o1.orderDetails;
            service.addOrder(o1);
        }

        private void buttonUpdate_Click(object sender, EventArgs e)
        {
           
            //service.modifyOrder(1, "1");
            FormUpdate formUpdate = new FormUpdate();
            formUpdate.ShowDialog();
            bindingSource1.DataSource = service.orderList.Where(s => s.OrderNo == formUpdate.no);
            foreach (OrderExer.Order order in service.orderList.Where(s => s.OrderNo == formUpdate.no))
            {
                bindingSource2.DataSource = order.orderDetails;
            }
                
            bool flag = false;//记录有没有修改订单
            foreach (OrderExer.Order order in service.orderList)
            {
                if (order.OrderNo == formUpdate.no)
                {
                    flag = true;
                    order.Cname = formUpdate.cname;
                    order.Customername= formUpdate.customername;
                    order.Amount = formUpdate.amount;
                    break;
                }
            }
            if (!flag)
            {
                MessageBox.Show("修改失败，未找到该订单");
            }
        }

        private void buttonDelete_Click(object sender, EventArgs e)
        {
            string no = "";
            FormDelete formdelete = new FormDelete();
            formdelete.ShowDialog();
            no = formdelete.no;
            int n = int.Parse(no);

            service.deleteOrder(n);
            
        }

        XmlSerializer xmlser = new XmlSerializer(typeof(OrderExer.Order[]));
        String xmlFileName = "order.xml";//定义xml文件名字
        //导出订单
        private void button2_Click(object sender, EventArgs e)
        {
            OrderExer.Order[] temp = new OrderExer.Order[service.orderList.Count];
            service.orderList.CopyTo(temp);

            //XML序列化

            FileStream fs = new FileStream(xmlFileName, FileMode.Create);
            xmlser.Serialize(fs, temp);
            fs.Close();

            //显示xml文本
            string xml = File.ReadAllText(xmlFileName);
            label3.Text = xml;
        }

        ///导入订单
        private void buttonImport_Click(object sender, EventArgs e)
        {
            List<OrderExer.Order> os = new List<OrderExer.Order>();
            string xml = "";
            using (FileStream fs = new FileStream(xmlFileName, FileMode.Open))
            {
                OrderExer.Order[] temp = (OrderExer.Order[])xmlser.Deserialize(fs);
                Array.ForEach(temp, p => xml =xml+ p.ToString()+"\n");
                Array.ForEach(temp, p =>os.Add(p));
                service.orderList = os;
            }
            label3.Text = xml;
        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void labelcname_Click(object sender, EventArgs e)
        {

        }

        private void buttonquit_Click(object sender, EventArgs e)
        {
            System.Environment.Exit(0);
        }
    }
}